package com.dev.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 实现过滤器
 * 1. 实现javax.servlet.Filter接口
 * 2. 重写方法
 * 3. 在doFilter方法中编写过滤逻辑（控制资源访问）
 * 4，配置过滤器，设置拦截路径
 */
// 拦截所有资源
@WebFilter("/myServlet")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter init");
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        
        // 处理请求数据
        System.out.println("MyFilter Before");

        filterChain.doFilter(servletRequest, servletResponse);

        // 处理响应数据
        System.out.println("MyFilter After");
    }

    @Override
    public void destroy() {

    }
}
